{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TR:用于QSAR建模的基于相似性的回归模型实操\n",
    "\n",
    "## 相关链接\n",
    "\n",
    "- [R. Zhang, D. Nolte, C. Sanchez-Villalobos, S. Ghosh, and R. Pal, “Topological regression as an interpretable and efficient tool for quantitative structure-activity relationship modeling,” Nat Commun, vol. 15, no. 1, p. 5072, Jun. 2024, doi: 10.1038/s41467-024-49372-0.](https://www.nature.com/articles/s41467-024-49372-0)\n",
    "- [源代码仓库](https://github.com/Ribosome25/TopoReg_QSAR)\n",
    "\n",
    "## 源码协议\n",
    "\n",
    "MIT开源协议.\n",
    "\n",
    "## 源码结构\n",
    "\n",
    "- DataExtraction/:从ChEMBL下载和处理数据,计算指纹;0_prepare_data_for_one_target.sh可以指定一个靶点,仅下载该靶点的相关数据\n",
    "- SampleDatesets/:ChEMBL的3个靶点示例数据,已提取相关数据,计算指纹并确定了按分子骨架分割的分子索引\n",
    "- utils/:命令行参数解析,采样,高斯核函数的包装\n",
    "- visualizations/:可视化脚本生成图片的位置\n",
    "- Ensemble_TR_pipeline.py:集成版TR流程\n",
    "- TR_pipeline.py:TR流程\n",
    "- visualize_NN_test_predictions.py:KNN,MLKR与TR的可视化比较脚本\n",
    "- visualize_TR_leadopt_pathways.py:先导物优化的可视化脚本\n",
    "\n",
    "## 环境准备\n",
    "\n",
    "```bash\n",
    "git clone https://github.com/Ribosome25/TopoReg_QSAR.git ~/git_develop/TopoReg # 克隆位置自行调整\n",
    "cd TopoReg\n",
    "conda env create --file environment.yml\n",
    "conda activate TopoReg\n",
    "```\n",
    "\n",
    "## 训练TR\n",
    "\n",
    "将数据文件所在路径作为参数传递给'TR_pipeline.py'的path参数即可训练TR并预测新结构的贡献:\n",
    "\n",
    "```py\n",
    "python TR_pipeline.py --path 'PATH to DATA'\n",
    "```\n",
    "\n",
    "脚本将输出模型性能中的NRMSE和Spearman相关性.具体选项:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "sys.path.append(os.path.abspath('..'))\n",
    "from note_utils.path import chdir\n",
    "\n",
    "tr_root=os.path.expanduser('~/git_develop/TopoReg') #仓库路径自行修改"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- -path PATH: str = 'SampleDatasets/ChEMBL/CHEMBL278/',包含数据文件的目录\n",
    "- -metric METRIC: str='tanimoto',对特征使用的默认距离度量方法\n",
    "- -split SPLIT: str='scaffold',用于评估的分割数据,'scaffold'或者'cv'\n",
    "- -seed SEED: int=2021,随机数种子\n",
    "- -cv_fold CV_FOLD: int=5,交叉验证倍数\n",
    "- -anchor_percentage ANCHOR_PERCENTAGE: float=0.5,锚点比例\n",
    "- -rbf_gamma RBF_GAMMA:float=0.5,RBF重构的gamma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前目录从 /home/regen/paper_notes/TopoRegQSAR 临时切换到 /home/regen/git_develop/TopoReg\n",
      "\n",
      "Scaffold TR Performance\n",
      "Spearman: 0.8345442589034985\n",
      "R2: 0.8614466743805966\n",
      "RMSE: 0.6902920473802873\n",
      "NRMSE: 0.37222751862188175\n",
      "当前目录切换回到 /home/regen/paper_notes/TopoRegQSAR\n"
     ]
    }
   ],
   "source": [
    "with chdir(tr_root) as (tmp,init):\n",
    "    os.system('python TR_pipeline.py -path SampleDatasets/ChEMBL/CHEMBL278/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "默认参数下输出结果应为:\n",
    "\n",
    "```log\n",
    "Scaffold TR Performance\n",
    "Spearman: 0.8345442589034985\n",
    "R2: 0.8614466743805971\n",
    "RMSE: 0.6902920473802862\n",
    "NRMSE: 0.3722275186218812\n",
    "```\n",
    "\n",
    "平均时间为0.5s.\n",
    "\n",
    "集成版TR参数类似:\n",
    "\n",
    "- mean_anchor_percentage: float=0.6,TR模型集合的平均锚点比例\n",
    "- std_anchor_percentage: float=0.2,TR模型集合的锚点比例标准差\n",
    "- num_TR_models = 30,TR模型集合的模型数\n",
    "\n",
    "\n",
    "## 可视化\n",
    "\n",
    "### 最近邻预测的可视化\n",
    "\n",
    "TR与KNN,MLKR可视化对比可以使用'visualize_NN_test_predictions.py':\n",
    "\n",
    "```py\n",
    "python visualize_NN_test_predictions.py\n",
    "```\n",
    "\n",
    "默认参数:\n",
    "\n",
    "- -path: str = 'SampleDatasets/ChEMBL/CHEMBL2734/'\n",
    "- -metric: str='tanimoto'\n",
    "- -split: str='cv'\n",
    "- -seed: int=2021\n",
    "- -cv_fold: int=5\n",
    "- -anchor_percentage: float=0.8\n",
    "- -k: int=5,KNN预测中的k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前目录从 /home/regen/paper_notes/TopoRegQSAR 临时切换到 /home/regen/git_develop/TopoReg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "visualize_NN_test_predictions.py:214: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.\n",
      "  fig1.tight_layout()\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前目录切换回到 /home/regen/paper_notes/TopoRegQSAR\n"
     ]
    }
   ],
   "source": [
    "with chdir(tr_root) as (tmp,init):\n",
    "    os.system('python visualize_NN_test_predictions.py -path SampleDatasets/ChEMBL/CHEMBL2734/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "预测结果如下:\n",
    "\n",
    "![KNN预测结果](visualize_NN_predictions_CHEMBL2734.png)\n",
    "\n",
    "平均时间为1.5min.\n",
    "\n",
    "### TR前导物优化可视化\n",
    "\n",
    "```py\n",
    "python visualize_TR_leadopt_pathways.py\n",
    "```\n",
    "\n",
    "默认参数:\n",
    "\n",
    "- path: str = 'SampleDatasets/ChEMBL/CHEMBL278/'\n",
    "- metric: str='tanimoto'\n",
    "- split: str='cv'\n",
    "- seed: int=2021\n",
    "- cv_fold: int=5\n",
    "- anchor_percentage: float=0.9\n",
    "- k: int=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前目录从 /home/regen/paper_notes/TopoRegQSAR 临时切换到 /home/regen/git_develop/TopoReg\n",
      "当前目录切换回到 /home/regen/paper_notes/TopoRegQSAR\n"
     ]
    }
   ],
   "source": [
    "with chdir(tr_root) as (tmp,init):\n",
    "    os.system('python visualize_TR_leadopt_pathways.py -path SampleDatasets/ChEMBL/CHEMBL278/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![先导物优化预测结果](lead_opt_pathway_CHEMBL278.png)\n",
    "\n",
    "平均预测时间约3s.\n",
    "\n",
    "## 数据提取\n",
    "\n",
    "DataExtraction文件夹下提供了从ChEMBL[1]中提取数据的方法.\n",
    "\n",
    "[1] Mendez, D., Gaulton, A., Bento, A. P., Chambers, J., De Veij, M., Félix, E., Magariños, M. P., Mosquera, J. F., Mutowo, P., Nowotka, M., Gordillo-Marañón, M., Hunter, F., Junco, L., Mugumbate, G., Rodriguez-Lopez, M., Atkinson, F., Bosc, N., Radoux, C. J., Segura-Cabrera, A., Hersey, A., … Leach, A. R. (2019). ChEMBL: towards direct deposition of bioassay data. Nucleic acids research, 47(D1), D930–D940. https://doi.org/10.1093/nar/gky1075\n",
    "\n",
    "TCNN下载自[[1](https://github.com/bigchem/transformer-cnn)],ChemProp下载自[[2](https://github.com/chemprop/chemprop)],其他模型使用sklearn和metric-learn包."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "TopoReg",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
